package com.yun.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yun.bean.YunPlayer;
import com.yun.service.PlayerService;
import com.yun.service.impl.PlayerServiceImpl;
import com.yun.util.Page;


@WebServlet("/PlayerServlet/*")
public class PlayerServlet extends HttpServlet {
	private PlayerService ps = new PlayerServiceImpl();
	
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String pathInfo = request.getPathInfo().substring(1);
		if("addPlayer".equals(pathInfo)) {
			addPlayer(request,response);
		} else if ("removeById".equals(pathInfo)) {
			removeById(request,response);
		} else if ("qurryById".equals(pathInfo)) {
			qurryById(request,response);
		} else if ("updPlayer".equals(pathInfo)) {
			updPlayer(request,response);
		} else if("listAllJson".equals(pathInfo)) {
			listAllJson(request,response);
		} else if("removeAll".equals(pathInfo)) {
			removeAll(request,response);
		}
	}
	private void removeAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String pids = request.getParameter("pids");
		//去掉两边[ ]
		pids = pids.substring(1, pids.length()-1);
		String[] pidsArr = pids.split(",");
		List<String> list=new ArrayList<>();
		for (int i = 0; i < pidsArr.length; i++) {
			//去掉每个id的""
			pidsArr[i]=pidsArr[i].substring(1, pidsArr[i].length()-1);
			list.add(pidsArr[i]);
		}
		int n = ps.removeAll(list);
		response.getWriter().print(n);
	}
	
	private void listAllJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		//接收分页参数：
		//起始索引
		int offset = Integer.valueOf(request.getParameter("offset"));
		//每页大小
		int limit = Integer.valueOf(request.getParameter("limit"));
		//学生列表数据:
		List<YunPlayer> studentList = ps.listAllByPage(offset, limit);
		//数据总条数:
		int total = ps.getTotal();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", studentList);
		Gson gson = new GsonBuilder().create();
		String json = gson.toJson(map);
		response.getWriter().print(json);		
	}

	private void updPlayer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p_id = request.getParameter("p_id");
		String p_name=request.getParameter("p_name");
		String p_car=request.getParameter("p_car");
		YunPlayer yunPlayer=new YunPlayer();
		yunPlayer.setP_id(p_id);
		yunPlayer.setP_name(p_name);
		yunPlayer.setP_car(p_car);
		ps.updPlayer(yunPlayer);
		request.getRequestDispatcher("/PlayerServlet/listAllByPage?pageNum=1").forward(request, response);
	}
	
	private void qurryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p_id =request.getParameter("p_id");
		YunPlayer yunPlayer=ps.selectById(p_id);
		request.setAttribute("yunPlayer", yunPlayer);
		request.getRequestDispatcher("/back/player/player_update.jsp").forward(request, response);
	}
	
	private void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p_id=request.getParameter("p_id");
		Boolean b=ps.removeById(p_id);
		if (b) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
	private void addPlayer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p_id = UUID.randomUUID().toString();
		String p_name=request.getParameter("p_name");
		String p_car=request.getParameter("p_car");
		YunPlayer yunPlayer=new YunPlayer();
		yunPlayer.setP_id(p_id);
		yunPlayer.setP_name(p_name);
		yunPlayer.setP_car(p_car);
		ps.addPlayer(yunPlayer);
		request.getRequestDispatcher("/back/player/player_add.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
